package com.example.demo.configuration;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * 将自定义拦截器加入到系统配置
 * 有两种写法
 * 一是 new 一个 UserInterceptor 对象
 * 二是 注入的方式
 */
@Configuration

public class AppConfig implements WebMvcConfigurer {

    @Autowired
    private UserInterceptor userInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // registry.addInterceptor(new UserInterceptor());

        registry.addInterceptor(userInterceptor)
                .addPathPatterns("/**") // 拦截所有请求
                .excludePathPatterns("/user/reg") // 登录页面不拦截
                .excludePathPatterns("/user/login") // 注册页面不拦截
                .excludePathPatterns("/user/getnum")
                .excludePathPatterns("/user/getstr")
                .excludePathPatterns("/user/usermsg")
                .excludePathPatterns("/reg.html")
                .excludePathPatterns("/login.html")
                .excludePathPatterns("/css/**")
                .excludePathPatterns("/editor.md/**")
                .excludePathPatterns("/img/**")
                .excludePathPatterns("/js/**")
                .excludePathPatterns("/image/**") // image 文件下所有的图片格式拦截
        ;
    }
}
